<!DOCTYPE html>
<html>
  <head>

    <script type="text/javascript" src="/static/jquery-1.5.2/jquery-1.5.2.min.js"></script>
    <script type="text/javascript" src="/static/flot-0.7/js/date.js"></script>
    <script type="text/javascript" src="/static/pmp/js/smaputil.js"></script>
    <script type="text/javascript" src="/static/pmp/js/comfortmodels.js"></script>
    <script type="text/javascript" src="/static/pmp/js/psychrometrics.js"></script>

    <title>Comfort Assessment</title>

    <script>

    $(document).ready(function(){
        main();
    });

    function main(){
        var interpd, p, q, su = smaputil;
        var start = new Date("March 30, 2012 00:00:00");
        var end = new Date("April 1, 2012 00:00:00");
        start = start.getTime();
        end = end.getTime();
        
        var wkd = true // document.getElementById("wkdays").checked;
        var sh = 8 // $("#startHour").val();
        var eh = 16 // parseInt($("#endHour").val());
        var tz = -7 // parseInt(sh.split(' ')[1].slice(0,1)+sh.split(' ')[1].slice(2,3));
        // sh = parseInt(sh.split(' ')[0]);
        
        var to_paths = ['/cbemtg/m6/c1','/cbemtg/m8/c1']
        var rh_paths = ['/cbemtg/m7/c1','/cbemtg/m9/c1']
        var paths = to_paths.concat(rh_paths)

        su.loadUuids(paths).done(function(uuid_dict){
            su.loadData(Object.values(uuid_dict), start, end).done(function(d){
                //console.log(d);
                d = su.resolveKeys(uuid_dict, d)
                //console.log(d);
                Object.keys(uuid_dict).forEach(function(el){
                    d[el] = su.filterTime(d[el], wkd, sh, eh, tz) 
                });
                su.transpose(d)
                for(i = 0; i < rh_paths.length ; i++){
                    interpd = []
                    p = rh_paths[i]
                    q = to_paths[i]
                    f = su.linterp(d[p][0], d[p][1])
                    for(j = 0; j < d[q][0].length ; j++){
                        interpd.push(f(d[q][0][j]))
                    }
                    d[p] = [d[q][0], interpd]
                }
                
                // icm-specific code
                var comfortData
                var icm1 = {}
                icm1.datetime = d[to_paths[0]][0]
                icm1.to = d[to_paths[0]][1]
                icm1.rh = d[rh_paths[0]][1]
                comfortData = calcComfort(icm1)
                icm1.ppd = comfortData[1]
                icm1.pmv = comfortData[0]
                var icm2 = {}
                icm2.datetime = d[to_paths[1]][0]
                icm2.to = d[to_paths[1]][1]
                icm2.rh = d[rh_paths[1]][1]
                comfortData = calcComfort(icm2)
                icm2.ppd = comfortData[1]
                icm2.pmv = comfortData[0]
                
            });
        });
        console.log(calcComfortLimits())
        
    }

    function calcComfort(data){ 
        var clo = 0.5  // $('#clo').val()
        var vel = 0.15
        var met = 1.2
        var x, to, rh, result1 = [], result2 = []
        var N = data.to.length      

        for (var i = 0; i < N; i++){
            to = FtoC(data.to[i])
            rh = data.rh[i]
            x = pmv(to, to, vel, rh, met, clo, 0)
            result1.push(x[0])
            result2.push(x[1])
        }
        return [result1, result2]
    }
   
    function calcComfortLimits(){
        var clo = 0.5
        var vel = 0.15
        var met = 1.2
       
        var fn = function(to){
            var x = pmv(to, to, vel, rh, met, clo, 0);
            return x[0]        
        }
        
        var rh = 0 
        var upper_rh0 = psy.bisect(0,200,fn,0.01,0.5)
        var lower_rh0 = psy.bisect(0,200,fn,0.01,-0.5)
        rh = 100
        var upper_rh1 = psy.bisect(0,200,fn,0.01,0.5)
        var lower_rh1 = psy.bisect(0,200,fn,0.01,-0.5)
        return [lower_rh0, upper_rh0, lower_rh1, upper_rh1]
    }

    function FtoC(x){ return (x - 32) * 5 / 9; }
 
    </script>
  </head>

<body>



</body>
</html>
